#define STANDARD
varying vec3 vViewPosition;
#ifdef USE_TRANSMISSION
varying vec3 vWorldPosition;
#endif
#include <common>
#include <batching_pars_vertex>
#include <uv_pars_vertex>
#include <displacementmap_pars_vertex>
#include <color_pars_vertex>
#include <fog_pars_vertex>
#include <normal_pars_vertex>
#include <morphtarget_pars_vertex>
#include <skinning_pars_vertex>
#include <shadowmap_pars_vertex>
#include <logdepthbuf_pars_vertex>
#include <clipping_planes_pars_vertex>

// void main() {
// ----------------------------------------------------------
varying vec3 vColor;
attribute vec4 aInstance;
attribute vec4 bInstance;

uniform float uTime;
uniform vec3 uColors[5];

varying float vColorful;

mat4 rotationMatrix(vec3 axis, float angle) {
    axis = normalize(axis);
    float s = sin(angle);
    float c = cos(angle);
    float oc = 1.0 - c;

    return mat4(oc * axis.x * axis.x + c,           oc * axis.x * axis.y - axis.z * s,  oc * axis.z * axis.x + axis.y * s,  0.0,
    oc * axis.x * axis.y + axis.z * s,  oc * axis.y * axis.y + c,           oc * axis.y * axis.z - axis.x * s,  0.0,
    oc * axis.z * axis.x - axis.y * s,  oc * axis.y * axis.z + axis.x * s,  oc * axis.z * axis.z + c,           0.0,
    0.0,                                0.0,                                0.0,                                1.0);
}

vec3 rotate(vec3 v, vec3 axis, float angle) {
    mat4 m = rotationMatrix(axis, angle);
    return (m * vec4(v, 1.0)).xyz;
}

float between(float center, float range, float val){
    return smoothstep(center - range, center, val) * smoothstep(center + range, center, val);
}
// ----------------------------------------------------------
void main(){
    #include <uv_vertex>
    #include <color_vertex>
    #include <morphcolor_vertex>
    #include <batching_vertex>
    #include <beginnormal_vertex>
    #include <morphnormal_vertex>
    #include <skinbase_vertex>
    #include <skinnormal_vertex>
    #include <defaultnormal_vertex>
    #include <normal_vertex>
    #include <begin_vertex>
    #include <morphtarget_vertex>
    #include <skinning_vertex>
    #include <displacementmap_vertex>

    // ------------------------------------------------------
    // #include <project_vertex>
    // transformed.x += aInstance.x;

    // 怎加时间可以使其转动起来
    float angle_circle = aInstance.x + uTime;
    // 粒子本身有一定角度
    transformed = rotate(transformed, normalize(vec3(0.2,0.4,0.6)), bInstance.x + uTime);
    // 只显示一般圆环
    float activation = between(PI * 1.5, PI * 0.5, mod(angle_circle, PI * 2.));
    // 粒子大小可以随机
    // transformed *= aInstance.w * activation;
    transformed *= aInstance.w * (activation * 2.5 + 1.);

    // 使其以圆环形式排列
    vec2 pos_circle = vec2(cos(angle_circle), sin(angle_circle)) * vec2(8.);
    transformed.xy += pos_circle;

    // xz轴随机扩展
    // float angle_cube = aInstance.y;
    // vec2 pos_cube = vec2(cos(angle_cube), sin(angle_cube)) * vec2(aInstance.z + 4.);
    // transformed.xz += pos_cube;

    float angle_cube = aInstance.y;
    // 粒子 xz轴随机扩展
    // vec3 pos_cube = vec3(0., cos(angle_cube), sin(angle_cube)) * (aInstance.z + 4.);
    // 粒子大小变化
    vec3 pos_cube = vec3(0., cos(angle_cube), sin(angle_cube)) * (aInstance.z + 4. - activation * 4.);
    // 粒子 y轴随机扩展
    pos_cube = rotate(pos_cube, vec3(0.,0.,-1.), angle_circle + PI + 0.5);
    transformed += pos_cube;

    vec4 mvPosition = vec4( transformed, 1.0 );
    #ifdef USE_INSTANCING
    mvPosition = instanceMatrix * mvPosition;
    #endif
    mvPosition = modelViewMatrix * mvPosition;

    // vColorful = 1.;
    vColorful = activation;
    if(bInstance.y == 2.) {
        // vColor = uColors[2];
        vColor = mix(uColors[2], uColors[1], 1. - activation);
    } else if(bInstance.y == 1.) {
        vColor = uColors[1];
    } else if(bInstance.y == 3.) {
        vColor = uColors[3];
    } else if(bInstance.y == 4.) {
        vColor = uColors[4];
    } else {
        // vColor = uColors[0];
        vColor = mix(uColors[0], uColors[1], position.x * 4.);
    }
    // vColor = vec3(1.);
    gl_Position = projectionMatrix * mvPosition;
    // ------------------------------------------------------

    #include <logdepthbuf_vertex>
    #include <clipping_planes_vertex>
    vViewPosition = - mvPosition.xyz;
    #include <worldpos_vertex>
    #include <shadowmap_vertex>
    #include <fog_vertex>
    #ifdef USE_TRANSMISSION
    vWorldPosition = worldPosition.xyz;
    #endif
}
